#include "camera.h"
#include "configmodel.h"
#include "errordialogmanager.h"
#include "qtconcurrentrun.h"
//int testHiconVMS();
Camera::Camera(QObject *parent) : QObject(parent), currentStatus(Initializing),CaptureHardwareType(CAPTUREHARDWARE_TPYE) {
    
    LogDebug << "Camera initialization started.";

    m_imageInfo["width"] = 0;
    m_imageInfo["height"] = 0;
    m_imageInfo["channels"] = 0;
    qRegisterMetaType<Camera::Status>("Camera::Status");
    //startCapture();
}

void Camera::setExposure(int exposure) {
}

Camera::~Camera() {

    stopCapture();
    if (getIsOpened()) {
        capRelease();
    }
    LogInfo << "Camera resources released.";
}

void Camera::startCapture() {

    LogDebug << "Attempting to start camera capture.";
    {
        if (!getIsOpened()) {
            capOpen();
            //cap.open(0);  // 0 默认打开第一个摄像头
        }
        setStatus(Idle);
        // if (getIsOpened()) {
        //     LogInfo << "Camera capture has been started.";
        //     setStatus(Idle);
        // } else {
        //     LogError << "Camera is not opened";
        //     setStatus(Error , "相机无法打开");
        // }
    }
}

void Camera::stopCapture() {
    LogDebug << "Attempting to stop camera capture.";
    // 停止捕获逻辑
    {
        if (getIsOpened()) {
            capRelease();
        }
    }
    LogInfo << "Camera capture stopped.";
    //setStatus(Stop);

}

void Camera::configDataChanged(QString Category, QString key)
{

}

bool Camera::getIsOpened()
{
    return 0;
}

void Camera::capRelease()
{
    return ;
}

void Camera::capOpen()
{
    return ;
}

cv::Mat Camera::getImage()
{
    cv::Mat frame;
    return frame;
}

void Camera::setStatus(Status newStatus,QString errorMess) {
    if (currentStatus != newStatus) {
        currentStatus = newStatus;
        LogDebug << "Camera status changed to: " << newStatus;
        emit statusChanged(newStatus);
    }
    if(newStatus == Error)
    {
        LogError << "setStatus : Error Because:" <<errorMess;
        lastError = errorMess;
        if (getIsOpened()) {
            capRelease();
        }
        emit errorOccurred(lastError);
    }
}

void Camera::logStepTime(const QString &step, qint64 time , bool recordTime) {
    if (recordTime) {
        stepTimes.insert(step, time);
        LogDebug << step << " duration: " << time << "ms";
    }
}

cv::Mat Camera::runCaptureOnce() {
    cv::Mat frame;
    if (recordTime) {
        stepTimes.clear();
    }
    if(getStatus() == Idle)
    {
        {
            frame = getImage();
        }
    }else
    {
        LogWarning << "Fail runCaptureOnce because CamaraStatus is" << getStatus();
    }
    sendImgInfo(frame);
    return frame;
}

void Camera::sendImgInfo(cv::Mat &frame)
{
    LogTrack << "Attempting to get ImgInfo";
    int width = frame.cols;
    int height = frame.rows;
    int channels = frame.channels();

    if (!m_imageInfo.contains("width")) {
        m_imageInfo["width"] = 0;
    }
    if (!m_imageInfo.contains("height")) {
        m_imageInfo["height"] = 0;
    }
    if (!m_imageInfo.contains("channels")) {
        m_imageInfo["channels"] = 0;
    }
    // 更新图像信息
    bool changed = false;
    if (m_imageInfo["width"].toInt() != width) {
        m_imageInfo["width"] = width;
        changed = true;
    }
    if (m_imageInfo["height"].toInt() != height) {
        m_imageInfo["height"] = height;
        changed = true;
    }
    if (m_imageInfo["channels"].toInt() != channels) {
        m_imageInfo["channels"] = channels;
        changed = true;
    }
    LogTrack << m_imageInfo["width"].toInt() << " www "
             <<m_imageInfo["height"].toInt()<< " www "
             <<m_imageInfo["channels"].toInt();
    // 如果图像尺寸发生变化，发送改变信号
    if (changed) {
        QVariantList list;
        list << width << height << channels;
        QVariant variant(list);
        ParamValue.setParameter("camera","cameraImgSize",variant);
        emit imageInfoChanged();
    }
}

